package edu.nctu.csie.jichang.database.dbinfo;

import java.util.ArrayList;
import java.util.List;

import edu.nctu.csie.jichang.database.util.DBException;
import edu.nctu.csie.jichang.database.util.StringUtil;

/**
 */
public class DataBaseInfo {
	
	public static final int TYPE_SQLSERVER		= 0x01;
	public static final int TYPE_POSTGRESQL	 	= 0x02;
	public static final int TYPE_ORACLE 		= 0x03;
	public static final int TYPE_MYSQL			= 0x04;
	
	private static final String[] TYPE_NAMES = new String[]{"Unknow","SqlServer","PostgreSQL","Oracle","MySQL"};

	private int type         = 0;
	private String name      = null;
	private String password  = null;
	private String host      = null;
	private String port      = null;
	private String descption = null;
	private String schema    = null;
	
	public DataBaseInfo(){
	}
	
	public DataBaseInfo(int type, String name, String password, String host,String port) {
		this.type = type;
		this.name = name;
		this.password = password;
		this.host = host;
		this.port = port;
		this.descption = TYPE_NAMES[type];
		checkType();
	}
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
		checkType();
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
	public String getDescption() {
		return descption;
	}
	public void setDescption(String descption) {
		this.descption = descption;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void checkType(){
		if(type < TYPE_SQLSERVER || type >TYPE_MYSQL){
			throw new DBException("unknow database type");
		}
	}
	@Override
	public String toString() {
		return "[type=" + type + ", name=" + name + ", password="+ password + ", host=" + host + ", port=" + port + ", descption=" + descption + "]";
	}

	public String getSchema() {
		return schema;
	}
	public void setSchema(String schema) {
		this.schema = schema;
	}
	
	public List<LoginInfo> getLoginInfos(){
		List<LoginInfo> tOut = new ArrayList<LoginInfo>();
		if(StringUtil.isNotEmptyOrSpace(schema)){
			for (String s : StringUtil.getStringSplit(schema, ",")) {
				LoginInfo tLoginInfo = new LoginInfo(this, s);
				tOut.add(tLoginInfo);
			}
		}
		return tOut;
	}
	
}